import inspect
from typing import List, Type


def create_class_diagram(classes: List[Type], output_file: str = 'class_diagram.md') -> None:
    """
    使用 mermaid 语法创建类图

    Args:
        classes: 要绘制的类列表
        output_file: 输出文件名
    """
    mermaid_code = ["```mermaid", "classDiagram"]

    for cls in classes:
        # 添加类名
        class_name = cls.__name__
        mermaid_code.append(f"    class {class_name} {{")

        # 添加属性
        for name, value in inspect.getmembers(cls):
            if not name.startswith('__') and not inspect.isfunction(value):
                attr_type = type(value).__name__
                mermaid_code.append(f"        {attr_type} {name}")

        # 添加方法
        for name, method in inspect.getmembers(cls, predicate=inspect.isfunction):
            if not name.startswith('__'):
                sig = inspect.signature(method)
                mermaid_code.append(f"        {name}{sig}")

        mermaid_code.append("    }")

        # 添加继承关系
        for base in cls.__bases__:
            if base.__name__ != 'object':
                mermaid_code.append(f"    {base.__name__} <|-- {class_name}")

    mermaid_code.append("```")

    # 写入文件
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write('\n'.join(mermaid_code))

    print(f"类图已生成到文件：{output_file}")
    print("您可以在支持 mermaid 的 Markdown 查看器中查看（如 VS Code、GitHub 等）")


if __name__ == '__main__':
    from request_config import RequestConfig
    from steam_api_client import SteamAPIClient

    # 创建类图
    create_class_diagram([RequestConfig, SteamAPIClient])